//
//  LoginView.m
//  KeleAppProject
//
//  Created by 周岑 on 2018/1/11.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "LoginView.h"
#import "LoginViewHeader.h"

@interface LoginView()

@property (nonatomic, copy) NSString *phoneNumStr;
@property (nonatomic, copy) NSString *codeStr;

@property (nonatomic, copy) NSString *loginType;  // account 账户   phonenumber  shoujihao


// 指向
@property (nonatomic, strong) UIView *verticalView;
@property (nonatomic, strong) UIImageView *secondLineImg;
@property (nonatomic, strong) UITextField *secondLineTF;
@property (nonatomic, strong) UIButton *forgetBtn;
@property (nonatomic, strong) UILabel *proTintLB;
@property (nonatomic, strong) UIButton *proBtn;
@property (nonatomic, strong) UIView *topLineView;
@end

@implementation LoginView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self loadSubviewOnLoginView];
    }
    return self;
}

- (void)loadSubviewOnLoginView{
    
    UIView *topLineView = [[UIView alloc]init];
    topLineView.backgroundColor = KLColor_B3;
    [self addSubview:topLineView];
    self.topLineView = topLineView;
    [topLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(1);
    }];
    
    // 电话
    UIView *phoneNumView = [[UIView alloc]init];
    phoneNumView.backgroundColor = [UIColor whiteColor];
    [self addSubview:phoneNumView];
    [phoneNumView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(CellFirstTop);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(CellViewH);
    }];
    
    UIImageView *phoneImgView = [[UIImageView alloc]init];
    phoneImgView.image = [UIImage imageNamed:@"login_icon_shouji"];
    phoneImgView.contentMode = UIViewContentModeScaleAspectFit;
    [phoneNumView addSubview:phoneImgView];
    [phoneImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneNumView).offset(23*HEIGHT);
        make.left.equalTo(phoneNumView).offset(CellImgLeft);
    }];
    
    UITextField *phoneTF = [[UITextField alloc]init];
    phoneTF.placeholder = PhonePlace;
    phoneTF.keyboardType = UIKeyboardTypeNumberPad;
    phoneTF.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14*WIDTH];
    phoneTF.textColor = KLColor_B2;
    phoneTF.textAlignment = NSTextAlignmentLeft;
    [phoneNumView addSubview:phoneTF];
    [[phoneTF rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(UITextField *phone) {
        self.phoneNumStr = phone.text;
        if (self.phoneNumStr.length == 11) {
            [self.sendCodeButton initialTime];
        }
    }];
    [phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(phoneImgView);
        make.width.mas_equalTo(200*WIDTH);
        make.left.equalTo(phoneImgView.mas_right).offset(CellTextfieldLeft);
    }];
    
    UIView *lineView_phone = [[UIView alloc]init];
    lineView_phone.backgroundColor = KLColor_B3;
    [phoneNumView addSubview:lineView_phone];
    self.verticalView = lineView_phone;
    [lineView_phone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(phoneImgView);
        make.right.equalTo(phoneNumView).offset(-VerticalLineRight);
        make.width.mas_equalTo(LineOne);
        make.height.mas_equalTo(LineH);
    }];
    
    SendCodeButton *codeBtn = [SendCodeButton buttonWithType:UIButtonTypeCustom];
    [codeBtn setTitle:GetCode forState:UIControlStateNormal];
    [codeBtn setTitleColor:KLColor_A1 forState:UIControlStateNormal];
    codeBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13*WIDTH];
    [phoneNumView addSubview:codeBtn];
    self.sendCodeButton = codeBtn;
    
    [[codeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
     
        if (self.codeDelegate) {
             NSString *phoneStr = self.phoneNumStr.length > 0 ? self.phoneNumStr:@"";
            [self.codeDelegate sendNext:@{@"phoneStr":phoneStr}];
        }
    }];
    
    self.sendCodeButton = codeBtn;
    [codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(phoneImgView);
        make.centerX.equalTo(phoneNumView.mas_right).offset(-60*WIDTH);
        
    }];
    
    UIView *bottomLine_phone = [[UIView alloc]init];
    bottomLine_phone.backgroundColor = KLColor_B3;
    [phoneNumView addSubview:bottomLine_phone];
    [bottomLine_phone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.centerX.equalTo(phoneNumView);
        make.width.mas_equalTo(BottoLineW);
        make.height.mas_equalTo(LineOne);
    }];
    
    
    // 验证码
    UIView *codeView = [[UIView alloc]init];
    codeView.backgroundColor = [UIColor whiteColor];
    [self addSubview:codeView];
    [codeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneNumView.mas_bottom);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(CellViewH);
    }];
    
    UIImageView *codeImgView = [[UIImageView alloc]init];
    codeImgView.image = [UIImage imageNamed:@"login_icon_yanzhengma"];
    codeImgView.contentMode = UIViewContentModeScaleAspectFit;
    [codeView addSubview:codeImgView];
    self.secondLineImg = codeImgView;
    [codeImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(codeView).offset(23*HEIGHT);
        make.left.equalTo(codeView).offset(CellImgLeft);
    }];
    
    UITextField *codeTF = [[UITextField alloc]init];
    codeTF.placeholder = CodePlace;
    codeTF.keyboardType = UIKeyboardTypeNumberPad;
    codeTF.textAlignment = NSTextAlignmentLeft;
    codeTF.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14*WIDTH];
    codeTF.textColor = KLColor_B2;
    [[codeTF rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(UITextField *code) {
        self.codeStr = codeTF.text;
    }];
    [codeView addSubview:codeTF];
    self.secondLineTF = codeTF;
    [codeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(codeImgView);
        make.width.mas_equalTo(200*WIDTH);

        make.left.equalTo(codeImgView.mas_right).offset(CellTextfieldLeft);
    }];
    
    UIView *bottomLine_code = [[UIView alloc]init];
    bottomLine_code.backgroundColor = KLColor_B3;
    [codeView addSubview:bottomLine_code];
    [bottomLine_code mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.centerX.equalTo(codeView);
        make.width.mas_equalTo(BottoLineW);
        make.height.mas_equalTo(LineOne);
    }];
    // 密码
    
    
    // 忘记密码按钮
    UIButton *forgetButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [forgetButton setTitle:ForgetPwd forState:UIControlStateNormal];
    forgetButton.hidden = YES;
    [forgetButton setTitleColor:KLColor_A1 forState:UIControlStateNormal];
    forgetButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13*WIDTH];
    [self addSubview:forgetButton];
    self.forgetBtn = forgetButton;
    [[forgetButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        if (self.delegateForget) {
            [self.delegateForget sendNext:nil];
        }
    }];
    [forgetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(codeView.mas_bottom).offset(10*HEIGHT);
        make.right.equalTo(self).offset(-CodeRight);
    }];
    
    // 登录按钮
    
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginBtn setTitle:PhoneLogin forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:18*WIDTH];
    [loginBtn setBackgroundImage:[UIImage createImageWithColor:KLColor_A1] forState:UIControlStateNormal];
    [self addSubview:loginBtn];
    self.loginBtn = loginBtn;
    [[loginBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *x) {
        if ([x.titleLabel.text isEqualToString:@"登录"]) {
            if (self.delegateAccount) {
                [self.delegateAccount sendNext:@{@"phoneStr":self.phoneNumStr,@"pwd":self.codeStr}];
            }
        }else if ([x.titleLabel.text isEqualToString:@"快捷登录"]){
            if (self.delegateSignal) {
                [self.delegateSignal sendNext:@{@"phoneStr":self.phoneNumStr,@"codeStr":self.codeStr}];
            }
        }
        
    }];
    
    RAC(loginBtn,enabled) = [RACSignal combineLatest:@[RACObserve(self, phoneNumStr),RACObserve(self, codeStr)] reduce:^id(NSString *phoneStr,NSString*codeStr){
        BOOL enable;
        enable = phoneStr.length>0&&codeStr.length>0;
        return @(enable);
    }];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(codeView.mas_bottom).offset(RegisterBtnTop);
        make.width.mas_equalTo(RegisterBtnW);
        make.height.mas_equalTo(RegisterBtnH);
    }];
    
    // 不同的登录类型的按钮
    UIButton *loginTypeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginTypeBtn setTitle:LoginTypeAcount forState:UIControlStateNormal];
    [loginTypeBtn setTitleColor:KLColor_A1 forState:UIControlStateNormal];
    loginTypeBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15*WIDTH];
    [self addSubview:loginTypeBtn];
    [[loginTypeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *typeBtn) {
        if ([typeBtn.titleLabel.text isEqualToString:AcountLogin]) {
            [typeBtn setTitle:PhoneLogin forState:UIControlStateNormal];
            [self changeStatusWithFlag:YES];
            self.secondLineImg.image = [UIImage imageNamed:@"login_icon_mima"];
            self.secondLineTF.placeholder = @"6-18位密码";
            [self.loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        }else if ([typeBtn.titleLabel.text isEqualToString:PhoneLogin]){
            [typeBtn setTitle:AcountLogin forState:UIControlStateNormal];
            [self changeStatusWithFlag:NO];
            self.secondLineImg.image = [UIImage imageNamed:@"login_icon_yanzhengma"];
            self.secondLineTF.placeholder = @"请输入验证码";
            [self.loginBtn setTitle:@"快捷登录" forState:UIControlStateNormal];
        }
    }];
    
    [loginTypeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(loginBtn.mas_bottom).offset(20*HEIGHT);
    }];
    // 协议
    UILabel *tintLb = [[UILabel alloc]init];
    tintLb.text = ProtocolTint;
    tintLb.textColor = KLColor_B4;
    tintLb.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12*WIDTH];
    [self addSubview:tintLb];
    self.proTintLB = tintLb;
    [tintLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(loginTypeBtn.mas_bottom).offset(RegisterProTop);
        make.left.equalTo(self).offset(RegisterProMargin);
    }];
    
    UIButton *protocolBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [protocolBtn setTitle:Protocol forState:UIControlStateNormal];
    [protocolBtn setTitleColor:KLColor_A1 forState:UIControlStateNormal];
    protocolBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12*WIDTH];
    [self addSubview:protocolBtn];
    self.protocolBtn = protocolBtn;
    self.proBtn = protocolBtn;
    [protocolBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(tintLb);
        make.right.equalTo(self).offset(-RegisterProMargin);
    }];
    
}

- (void)layoutSublayersOfLayer:(CALayer *)layer{
    [super layoutSublayersOfLayer:layer];
    [self.loginBtn layoutIfNeeded];
    [self.protocolBtn layoutIfNeeded];
    [self.loginBtn setRadiusWithValue:22*HEIGHT];
    [self.protocolBtn addBottomLineWithColor:KLColor_A1 WithHeight:20*HEIGHT];
}

- (void)changeStatusWithFlag:(BOOL)flag{
    self.verticalView.hidden = flag;
    self.sendCodeButton.hidden = flag;
    self.forgetBtn.hidden = !flag;
    self.proTintLB.hidden = flag;
    self.proBtn.hidden = flag;
    self.topLineView.hidden = flag;

}

@end
